#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
#import resource
import logging
import math
# from PIL import Image
# import PIL
try:
    from PIL import Image
except:
    import Image

# #一共多少张图片待合并 b  ################################################
# total_num = 28
# #指定单张小图的宽和高
# pic_w = 432
# pic_h = 769
# #指定合并后的大图每行排多少张单图
# row = 25
# #?无需调整
# one_pic_num = 250
# colum = one_pic_num / row
# #待合并单图所在的文件夹和文件名前缀
# file_ex = "b/b_"
# #合并后大图的文件前缀
# final_ex = "video_b_"
# #待合并单图开始序号
# start_index = 1

#一共多少张图片待合并 bird ################################################
# total_num = 3
# #指定单张小图的宽和高
# pic_w = 83
# pic_h = 254
# #指定合并后的大图每行排多少张单图
# row = 3
# #?无需调整
# one_pic_num = 250
# colum = one_pic_num / row
# #待合并单图所在的文件夹和文件名前缀
# file_ex = "bird/bird"
# #合并后大图的文件前缀
# final_ex = "video_bird_"
# #待合并单图开始序号
# start_index = 1

# #一共多少张图片待合并 c ################################################
# total_num = 35
# #指定单张小图的宽和高
# pic_w = 432
# pic_h = 769
# #指定合并后的大图每行排多少张单图
# row = 25
# #?无需调整
# one_pic_num = 250
# colum = one_pic_num / row
# #待合并单图所在的文件夹和文件名前缀
# file_ex = "c/c_"
# #合并后大图的文件前缀
# final_ex = "video_c_"
# #待合并单图开始序号
# start_index = 1

# #一共多少张图片待合并 d ################################################
# total_num = 57
# #指定单张小图的宽和高
# pic_w = 432
# pic_h = 769
# #指定合并后的大图每行排多少张单图
# row = 25
# #?无需调整
# one_pic_num = 250
# colum = one_pic_num / row
# #待合并单图所在的文件夹和文件名前缀
# file_ex = "d/d_"
# #合并后大图的文件前缀
# final_ex = "video_d_"
# #待合并单图开始序号
# start_index = 1

#一共多少张图片待合并 e ################################################
total_num = 57
#指定单张小图的宽和高
pic_w = 432
pic_h = 769
#指定合并后的大图每行排多少张单图
row = 25
#?无需调整
one_pic_num = 250
colum = one_pic_num / row
#待合并单图所在的文件夹和文件名前缀
file_ex = "e/e_"
#合并后大图的文件前缀
final_ex = "video_e_"
#待合并单图开始序号
start_index = 1

total_new_pic_num = int(math.ceil(float(total_num) / float(one_pic_num)))
def setPics(page):
    pics = []
    if page < total_new_pic_num - 1:
        p = one_pic_num
        colum = one_pic_num / row
    else:
        p = total_num - (total_new_pic_num - 1) * one_pic_num
        colum = int(math.ceil(float(p) / float(row)))

    # print colum
    # print page,total_new_pic_num,p

    # return

    for index in range(p):

        index += start_index
        index += page * one_pic_num
        index_str = str(index)
        # if index < 10:
        #     index_str = "000" + str(index)
        # elif index >= 10 and index < 100:
        #     index_str = "00" + str(index)
        # else:
        #     index_str = "0" + str(index)
        pics.append(file_ex + index_str + ".jpg")

    images = map(Image.open, pics)


    total_width = row * pic_w
    total_height = colum * pic_h


    new_im = Image.new('RGB', (total_width, total_height))

    x_offset = 0
    y_offset = 0
    for i in range(len(images)):
        im = images[i]
        mod_x = i % row
        mod_y = i / row
        x_offset = mod_x * pic_w
        y_offset = mod_y * pic_h
        new_im.paste(im, (x_offset, y_offset))

    new_im.save(final_ex + str(page) + '.jpg')



# print total_new_pic_num

for i in range(int(total_new_pic_num)):
    # print i
    setPics(i)

# setPics()
# for im in images:
#   new_im.paste(im, (x_offset,y_offset))
#   mod_x = im % row 
#   if x_offset < :
#   	x_offset += im.size[0]
#   else:


# new_im = Image.new('RGB', (total_width, max_height))

# x_offset = 0
# for im in images:
#   new_im.paste(im, (x_offset,0))
#   x_offset += im.size[0]

# new_im.save('test.jpg')
